
const http = require('http')
const https = require('https')

// const url = 'https://www.7-zip.org/a/7z1900.exe';
// const url = "https://npm.taobao.org/mirrors/node/v14.7.0/node-v14.7.0-win-x64.zip";
const url = "https://cdn.npm.taobao.org/dist/node/v14.7.0/node-v14.7.0-win-x64.zip";

function req(url) {
    const parsed = new URL(url);
    const method = 'get';
    let h;
    if (parsed.protocol === 'https:') {
        h = https
    } else if (parsed.protocol === 'http:') {
        h = http
    } else {
        throw new Error(`Unknown protocol, ${parsed.protocol}`)
    }
    const request = {
        path: parsed.pathname + parsed.search,
        port: parsed.port,
        method: method,
        headers: {},
        hostname: parsed.hostname
    }

    return new Promise(function (resolve, reject) {
        const req = h.request(request, (res) => {
            console.log('状态码:', res.statusCode);
            // console.log('请求头:', res.headers);
            resolve(res.statusCode)
            // res.on('data', (d) => {
            // process.stdout.write(d);
            // });
        });

        req.on('error', (e) => {
            console.error(e);
            reject(e)
        });
        req.end();
    });
}

var arr = []
for (let i = 0; i < 100; i++) {
    arr.push(req(url));
}

(async function () {
    console.time('req')
    await Promise.all(arr);
    console.timeEnd('req')
})()